﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using BrokerBase.Lib.Person;
using BrokerBase.Lib.Core;
using NHibernate;

namespace Broker.View.PersonView
{
    /// <summary>
    /// HumanRelationForm.xaml 的交互逻辑
    /// </summary>
    public partial class HumanRelationForm 
    {

        private IHumanRelation relation;
        private Action callBack;
        private bool isLockPerson1;

        public HumanRelationForm(IHumanRelation relation,Action callBack,bool isLockPerson1)
        {
            InitializeComponent();
            this.relation = relation;
            this.callBack = callBack;
            this.isLockPerson1 = isLockPerson1;

            this.Loaded += new RoutedEventHandler(HumanRelationForm_Loaded);
        }

        void HumanRelationForm_Loaded(object sender, RoutedEventArgs e)
        {
            DataContext = relation;

            LoadRelationTypes();

            if (relation.Person1 != null && isLockPerson1)
            {
                txtPerson1.IsReadOnly = true;
                btnSelPerson1.IsEnabled = false;
            }

            
        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            if (Verify())
            {
                try
                {
                    relation.BizHashCode = relation.GetBizHashCode();

                    relation.RecordTime = DateTime.Now;
                    IBaseService<IHumanRelation> service = new BaseService<IHumanRelation>(LibGlobal.HbmSessionFactory.GetCurrentSession());

                    service.SaveOrUpdate(relation as HumanRelation);


                    if (callBack != null)
                    {
                        callBack();
                    }

                    MessageBox.Show("关系保存成功!", "提示");
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }

        private void btnExit_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }

        private bool Verify()
        {
            if (relation.Person1 == null)
            {
                MessageBox.Show("关系人1不能为空!", "提示");
                return false;
            }

            if (relation.Person2 == null)
            {
                MessageBox.Show("关系人2不能为空!", "提示");
                return false;
            }

            if (relation.Person1 == relation.Person2)
            {
                MessageBox.Show("关系人1不能等于关系人2!", "提示");
                return false;
            }

            if (relation.RelationType == null)
            {
                MessageBox.Show("关系类型不能为空!", "提示");
                return false;
            }

            using (ISession sess = LibGlobal.HbmSessionFactory.OpenSession())
            {
                HumanRelation hr = sess.CreateQuery("from HumanRelation where BizHashCode="+relation.GetBizHashCode()).UniqueResult<HumanRelation>();

                if (hr != null && (relation as HumanRelation).Id !=hr.Id )
                {
                    MessageBox.Show(relation.Person1.Name + " 与 " + relation.Person2.Name + "的" + relation.RelationType.Name + "关系已经存在!", "提示");
                    return false;
                }
            }

            return true;
        }

        private void btnSelPerson1_Click(object sender, RoutedEventArgs e)
        {
            PersonView.PersonListPage plp = new PersonView.PersonListPage();

            try
            {

                IPerson p = plp.SelectByHQL(null, this.Owner, 400, 600);

                this.relation.Person1 = p;

                txtPerson1.Text = p == null ? string.Empty : p.Name;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "错误提示");
            }
        }

        private void btnSelPerson2_Click(object sender, RoutedEventArgs e)
        {
            PersonView.PersonListPage plp = new PersonView.PersonListPage();

            try
            {

                IPerson p = plp.SelectByHQL(null, this.Owner, 400, 600);

                this.relation.Person2 = p;

                txtPerson2.Text = p == null ? string.Empty : p.Name;

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "错误提示");
            }
        }

        private void LoadRelationTypes()
        {
            using (ISession sess = LibGlobal.HbmSessionFactory.OpenSession())
            {
                IList<IHumanRelationType> ls = sess.CreateQuery("from HumanRelationType").List<IHumanRelationType>();

                cmbRelationType.ItemsSource = ls;
            }
        }
    }
}
